from src import Node,Wrpc
from src import fun_read_excel
from tqdm import tqdm
from time import time

def configNode_main():
    try:
        error_ip = []
        data = list(fun_read_excel("./WR_config_data.xlsx"))
        _time = int(time())
        with open("./output/output_"+str(_time)+".csv","w",encoding="utf-8") as f:
            f.write("ip,版本号,版本创建时间,DTx,DRx,Alpha,MC0使能,MC1使能,UTC使能,联锁使能,心跳使能,设备号,心跳组,心跳周期,心跳偏置,载板类型,PC IP,PC MAC,M1 IP,M1 MAC,M2 IP,M2 MAC\n")
            for index in tqdm(range(len(data))):
                ip = 0
                wrpcVersionSwVersion = 0
                wrpcVersionSwBuildDate = 0
                impCtrlMC0Ena = 0
                impCtrlMC1Ena = 0
                impCtrlUtcCodingEna = 0
                impCtrlInterlockEna = 0
                impCtrlHeartbeatEna = 0
                impCtrlDeviceNum = 0
                impCtrlHeartGroup = 0
                impCtrlHeartCycle = 0
                impCtrlHeartOffset = 0
                impCtrlCarrier = 0
                impReadPCIP = 0
                impReadPCIP = 0
                impReadM1IP = 0
                impReadM1MAC = 0
                impReadM2IP = 0
                impReadM2MAC = 0

                if data[index][0] == "ip":
                    continue
                try:
                    ip = str("192.168.1.") + str(int(data[index][0]))
                    impCtrlDeviceNum = int(data[index][1])
                    impCtrlHeartGroup = int(data[index][2])
                    impCtrlHeartCycle = int(data[index][3])
                    impCtrlHeartOffset = int(data[index][4])
                    impCtrlCarrier = int(data[index][5])
                    if not data[index][6] == "":
                        PC_IP = data[index][6]
                    if not data[index][7] == "":
                        PC_MAC = data[index][7]

                    if not data[index][8] == "":
                        M1_IP = data[index][8]
                    if not data[index][9] == "":
                        M1_MAC = data[index][9]

                    if not data[index][10] == "":
                        M2_IP = data[index][10]
                    if not data[index][11] == "":
                        M2_MAC = data[index][11]
                    try:
                        wrpcVersionGroup_data = Wrpc.wrpcVersionGroup().walk_wrpcVersionGroup(ip=ip)
                        wrpcVersionSwVersion = wrpcVersionGroup_data["wrpcVersionSwVersion"]
                        wrpcVersionSwBuildDate = wrpcVersionGroup_data["wrpcVersionSwBuildDate"]
                        
                        impCtrlMENA = Node.impCtrlMENA()
                        impBPSConfig = Node.impBPSConfig()
                        impCtrlApply = Node.impCtrlApply()
                        impNetAddrConfig = Node.impNetAddrConfig()
                        
                        impCtrlApply.set_impCtrlApply(ip=ip,value=1)
                        
                        impBPSConfig.set_impCtrlDeviceNum(ip=ip,value=impCtrlDeviceNum)
                        impBPSConfig.set_impCtrlHeartGroup(ip=ip,value=impCtrlHeartGroup)
                        impBPSConfig.set_impCtrlHeartCycle(ip=ip,value=impCtrlHeartCycle)
                        impBPSConfig.set_impCtrlHeartOffset(ip=ip,value=impCtrlHeartOffset)
                        impBPSConfig.set_impCtrlCarrier(ip=ip,value=impCtrlCarrier)

                        impCtrlApply.set_impCtrlApply(ip=ip,value=15)
                        impBPSConfig_data = impBPSConfig.walk_impBPSConfig(ip=ip)
                        impCtrlDeviceNum = impBPSConfig_data["impCtrlDeviceNum"]
                        impCtrlHeartGroup = impBPSConfig_data["impCtrlHeartGroup"]
                        impCtrlHeartCycle = impBPSConfig_data["impCtrlHeartCycle"]
                        impCtrlHeartOffset = impBPSConfig_data["impCtrlHeartOffset"]
                        impCtrlCarrier = impBPSConfig_data["impCtrlCarrier"]

                        impCtrlMENA.set_impCtrlMC0Ena(ip=ip,value=1)
                        impCtrlMENA.set_impCtrlMC1Ena(ip=ip,value=1)
                        impCtrlMENA.set_impCtrlUtcCodingEna(ip=ip,value=1)
                        impCtrlMENA.set_impCtrlInterlockEna(ip=ip,value=1)
                        impCtrlMENA.set_impCtrlHeartbeatEna(ip=ip,value=1)
                        impCtrlApply.set_impCtrlApply(ip=ip,value=11)

                        impCtrlMENA_data = impCtrlMENA.walk_impCtrlMENA(ip=ip)
                        impCtrlMC0Ena = impCtrlMENA_data["impCtrlMC0Ena"]
                        impCtrlMC1Ena = impCtrlMENA_data["impCtrlMC1Ena"]
                        impCtrlUtcCodingEna = impCtrlMENA_data["impCtrlUtcCodingEna"]
                        impCtrlInterlockEna = impCtrlMENA_data["impCtrlInterlockEna"]
                        impCtrlHeartbeatEna = impCtrlMENA_data["impCtrlHeartbeatEna"]

                        wrpcSfpTable = Wrpc.wrpcSfpTable().walk_wrpcSfpTable(ip=ip)
                        if len(wrpcSfpTable) == 0:
                            DTx = 0
                            DRx = 0
                            Alpha = 0
                        else:
                            DTx = wrpcSfpTable["wrpcSfpDeltaTx"]
                            DRx = wrpcSfpTable["wrpcSfpDeltaRx"]
                            Alpha = wrpcSfpTable["wrpcSfpAlpha"]

                        
                        impNetAddrConfig.set_impWriteMAC(ip=ip,value=PC_MAC)
                        impCtrlApply.set_impCtrlApply(ip=ip,value=22)
                        impNetAddrConfig.set_impWriteIP(ip=ip,value=PC_IP)
                        impCtrlApply.set_impCtrlApply(ip=ip,value=21)

                        impNetAddrConfig.set_impWriteMAC(ip=ip,value=M1_MAC)
                        impCtrlApply.set_impCtrlApply(ip=ip,value=24)
                        impNetAddrConfig.set_impWriteIP(ip=ip,value=M1_IP)
                        impCtrlApply.set_impCtrlApply(ip=ip,value=23)
                        
                        impNetAddrConfig.set_impWriteMAC(ip=ip,value=M2_MAC)
                        impCtrlApply.set_impCtrlApply(ip=ip,value=26)
                        impNetAddrConfig.set_impWriteIP(ip=ip,value=M2_IP)
                        impCtrlApply.set_impCtrlApply(ip=ip,value=25)
                        
                        

                        impNetAddrConfig_data = impNetAddrConfig.walk_impNetAddrConfig(ip=ip)
                        impReadPCIP = impNetAddrConfig_data["impReadPCIP"]
                        impReadPCMAC = impNetAddrConfig_data["impReadPCMAC"]
                        impReadM1IP = impNetAddrConfig_data["impReadM1IP"]
                        impReadM1MAC = impNetAddrConfig_data["impReadM1MAC"]
                        try:
                            impReadM2IP = impNetAddrConfig_data["impReadM2IP"]
                            impReadM2MAC = impNetAddrConfig_data["impReadM2MAC"]
                        except Exception as e:
                            pass
                            print(f"\nip:{ip}\t初始化失败,请检查\n固件版本:\t{wrpcVersionSwVersion}\n固件创建时间:\t{wrpcVersionSwBuildDate}") 
                        
                        
                        impCtrlApply.set_impCtrlApply(ip=ip,value=3)
                        f.write(f"{ip},{wrpcVersionSwVersion},{wrpcVersionSwBuildDate},{DTx},{DRx},{Alpha},{impCtrlMC0Ena},{impCtrlMC1Ena},{impCtrlUtcCodingEna},{impCtrlInterlockEna},{impCtrlHeartbeatEna},{impCtrlDeviceNum},{impCtrlHeartGroup},{impCtrlHeartCycle},{impCtrlHeartOffset},{impCtrlCarrier},{impReadPCIP},{impReadPCMAC},{impReadM1IP},{impReadM1MAC},{impReadM2IP},{impReadM2MAC}\n")
                    except Exception as e:
                        print(e)
                        error_ip.append(ip)
                        #print(f"ip:{ip}\t初始化失败,请检查网络")
                        # print(ip,impCtrlDeviceNum,impCtrlHeartGroup,impCtrlHeartCycle,impCtrlHeartOffset,impCtrlCarrier,PC_IP,PC_MAC,M1_IP,M1_MAC,M2_IP,M2_MAC)
                except Exception as e:
                    print("ip:{ip}\t初始化失败,请检查配置文件")
            f.close()
        print(f"ip:{str(error_ip)}\t初始化失败,请检查网络")
    except Exception as e:
        print("请检查WR_config_data.xlsx文件是否存在")
